unit Class_ADDX;

interface
uses
  Classes,SysUtils,Uni,UniEngine,DB;

type
  TADDX=class(TUniEngine)
  public
    UNITLINK: string;
    ADDXIDEX: Integer;
    CARDIDEX: Integer;
    ADDXSTRM: TMemoryStream;
    ADDXNAME: string;
    ADDXMEMO: string;
  protected
    procedure SetParameters;override;
    function  GetStrInsert:string;override;
    function  GetStrUpdate:string;override;
    function  GetStrDelete:string;override;
  public
    function  GetStrsIndex:string;override;
  public
    function  GetNextIdex:Integer;overload;
    function  GetNextIdex(AUniConnection:TUniConnection):Integer;overload;
  public
    function  CheckExist(AUniConnection:TUniConnection):Boolean;override;
  public
    class function  ReadDS(AUniQuery:TUniQuery):TUniEngine;override;
    class function  CopyIt(AADDX:TADDX):TADDX;overload;        
    class procedure CopyIt(AADDX:TADDX;var Result:TADDX);overload;       
  end;

implementation

uses
  Unit1;

{ TADDX }
procedure TADDX.SetParameters;
begin
  inherited;
  with FUniSQL.Params do
  begin
    case FOptTyp of
      otAddx:
      begin
        ParamByName('UNIT_LINK').Value := UNITLINK;
        ParamByName('ADDX_IDEX').Value := ADDXIDEX;
        ParamByName('CARD_IDEX').Value := CARDIDEX;
        ParamByName('ADDX_STRM').LoadFromStream(ADDXSTRM, ftBlob);        
        ParamByName('ADDX_NAME').Value := ADDXNAME;
        ParamByName('ADDX_MEMO').Value := ADDXMEMO; 
      end;
      otEdit:
      begin
        ParamByName('UNIT_LINK').Value := UNITLINK;
        ParamByName('ADDX_IDEX').Value := ADDXIDEX;
        ParamByName('CARD_IDEX').Value := CARDIDEX;
        ParamByName('ADDX_STRM').LoadFromStream(ADDXSTRM, ftBlob);
        ParamByName('ADDX_NAME').Value := ADDXNAME;
        ParamByName('ADDX_MEMO').Value := ADDXMEMO; 
      end;  
      otDelt:
      begin
        ParamByName('UNIT_LINK').Value := UNITLINK;
        ParamByName('ADDX_IDEX').Value := ADDXIDEX;
 
      end;  
    end;
  end;
end;

function TADDX.CheckExist(AUniConnection: TUniConnection): Boolean;
begin
  Result:=CheckExist('TBL_ADDX',['UNIT_LINK',UNITLINK,'ADDX_IDEX',ADDXIDEX],AUniConnection);
end;

function TADDX.GetNextIdex: Integer;
begin

end;

function TADDX.GetNextIdex(AUniConnection: TUniConnection): Integer;
begin

end;

function TADDX.GetStrDelete: string;
begin
  Result:='DELETE FROM TBL_ADDX WHERE   UNIT_LINK=:UNIT_LINK AND ADDX_IDEX=:ADDX_IDEX';
end;

function TADDX.GetStrInsert: string;
begin
  Result:='INSERT INTO TBL_ADDX (UNIT_LINK,ADDX_IDEX,CARD_IDEX,ADDX_STRM,ADDX_NAME,ADDX_MEMO) VALUES (:UNIT_LINK,:ADDX_IDEX,:CARD_IDEX,:ADDX_STRM,:ADDX_NAME,:ADDX_MEMO)';
end;

function TADDX.GetStrsIndex: string;
begin
  Result:=Format('%S-%D',[UnitLink,AddxIdex]);
end;

function TADDX.GetStrUpdate: string;
begin
  Result:='UPDATE  TBL_ADDX SET'
         +'    CARD_IDEX=:CARD_IDEX,'
         +'    ADDX_STRM=:ADDX_STRM,'
         +'    ADDX_NAME=:ADDX_NAME,'
         +'    ADDX_MEMO=:ADDX_MEMO'
         +'    WHERE UNIT_LINK=:UNIT_LINK'
         +'    AND   ADDX_IDEX=:ADDX_IDEX';
end;

class function TADDX.ReadDS(AUniQuery: TUniQuery): TUniEngine;
begin
  Result:=TADDX.Create;
  with TADDX(Result) do
  begin
    UNITLINK:=AUniQuery.FieldByName('UNIT_LINK').AsString;
    ADDXIDEX:=AUniQuery.FieldByName('ADDX_IDEX').AsInteger;
    CARDIDEX:=AUniQuery.FieldByName('CARD_IDEX').AsInteger;
    ADDXSTRM:=TMemoryStream.Create;
    TBlobField(AUniQuery.FieldByName('ADDX_STRM')).SaveToStream(ADDXSTRM);
    //ADDXSTRM:=AUniQuery.FieldByName('ADDX_STRM').AsInteger;
    ADDXNAME:=AUniQuery.FieldByName('ADDX_NAME').AsString;
    ADDXMEMO:=AUniQuery.FieldByName('ADDX_MEMO').AsString;
  end;  
end;

class function  TADDX.CopyIt(AADDX: TADDX): TADDX;
begin
  Result:=TADDX.Create;
  Result.UNITLINK:=AADDX.UNITLINK;
  Result.ADDXIDEX:=AADDX.ADDXIDEX;
  Result.CARDIDEX:=AADDX.CARDIDEX;
  Result.ADDXSTRM:=AADDX.ADDXSTRM;
  Result.ADDXNAME:=AADDX.ADDXNAME;
  Result.ADDXMEMO:=AADDX.ADDXMEMO;
end;

class procedure TADDX.CopyIt(AADDX:TADDX;var Result:TADDX);
begin
  if Result=nil then Exit;
  Result.UNITLINK:=AADDX.UNITLINK;
  Result.ADDXIDEX:=AADDX.ADDXIDEX;
  Result.CARDIDEX:=AADDX.CARDIDEX;
  Result.ADDXSTRM:=AADDX.ADDXSTRM;
  Result.ADDXNAME:=AADDX.ADDXNAME;
  Result.ADDXMEMO:=AADDX.ADDXMEMO;
end;

end.

